﻿using System.Collections.Generic;
using RoMBot.MemoryLib;

namespace RoMBot.Classes
{
    public class PlayerSkills
    {
        private static List<PlayerSkill> _skills = new List<PlayerSkill>();

        public static List<PlayerSkill> Skills
        {
            get
            {
                return PlayerSkills._skills;
            }
            set
            {
                PlayerSkills._skills = value;
            }
        }

        static PlayerSkills()
        {
        }

        public static void Load()
        {
            var proc = Bot.Instancia.ProcId;
            PlayerSkills._skills.Clear();
            var skillsTableTabSize = 0x10;
            var skillSize = 0x4c;

            for ( int tab = 2; tab < 6; tab++ )
            {
                var tabBaseAddress = Memory.ReadRepeat<uint>( proc, Addresses.skillsTableBase + skillsTableTabSize * ( tab - 1 ) + Addresses.skillsTableTabStartAddress_offset );
                var tabEndAddress = Memory.ReadRepeat<uint>( proc, Addresses.skillsTableBase + skillsTableTabSize * ( tab - 1 ) + Addresses.skillsTableTabEndAddress_offset );
                if ( tabBaseAddress != 0 && tabEndAddress != 0 )
                {
                    for ( int num = 1; num < ( tabEndAddress - tabBaseAddress ) / skillSize; num++ )
                    {
                        var skilladdress = tabBaseAddress + ( num - 1 ) * skillSize;
                        var skill = new PlayerSkill( ( int ) skilladdress );
                        skill.SkillTab = tab;
                        skill.SkillNum = num;
                        if ( !string.IsNullOrWhiteSpace( skill.Name ) )
                        {
                            _skills.Add( skill );
                        }
                    }
                }
            }
        }

        private PlayerSkill GetSkillInfo( int address )
        {
            var tmp = new PlayerSkill( address );
            var proc = Bot.Instancia.ProcId;
            tmp.Address = address;
            tmp.Id = Memory.ReadRepeat<int>( proc, address );
            tmp.Name = MemoryTable.GetIdName( tmp.Id );
            tmp.TPToLevel = Memory.ReadRepeat<int>( proc, address + Addresses.skillTPToLevel_offset );
            tmp.Level = Memory.ReadRepeat<int>( proc, address + Addresses.skillLevel_offset );
            tmp.Aslevel = Memory.ReadRepeat<int>( proc, address + Addresses.skillAsLevel_offset );
            tmp.BaseItemAddress = MemoryTable.GetItemAddress( tmp.Id );
            return tmp;
        }
    }
}